package com.cpc.mback.controller;


import com.cpc.mback.RespEntity;
import com.cpc.mback.entity.User;
import com.cpc.mback.service.MainService;
import com.cpc.mback.service.RecordService;
import com.cpc.mback.service.UploadService;
import com.cpc.mback.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(value = "用户接口", tags = "用户接口", description = "没有description")
@RequestMapping("/api/user")
public class UserController {



    @Autowired
    MainService mainService;

    @Autowired
    UploadService uploadService;
    @Autowired
    RecordService recordService;
    @Autowired
    UserService userService;


    @ApiOperation(value = "验证是否注册", notes = "参数\n" +
            " ")
    @GetMapping("/verify")
    public ResponseEntity verify(@RequestParam String openId) {

        //return userService.checkRegistered(openId);
        if (userService.checkRegistered(openId)){
            return ResponseEntity.status(200).body(RespEntity.code(200).body("用户已注册").build());
        }else {
            return ResponseEntity.status(400).body(RespEntity.code(400).body("用户未注册").build());
        }
    }
    @ApiOperation(value = "上传用户信息", notes = "参数\n" +
            " ")
    @PostMapping("/sign-up")
    public ResponseEntity postUser(@RequestBody UserPayload payload) {

        User user = userService.SignUp(payload.openId,payload.name,payload.gender,payload.inviteCode,payload.styleSign,payload.intro,payload.avatar);
        return ResponseEntity.status(200).body(RespEntity.code(200).body(user).build());

    }

    @ApiOperation(value = "受询者认证", notes = "参数 完整的payload\n" +
            " ")
    @PostMapping("/verti")
    public ResponseEntity certificateUser(@RequestBody VipUserPayload payload ) {

        /*return userService.Certificate( payload.openId,payload.name,payload.weChatNumber,
                payload.phone, payload.email,
                payload.fee,payload.spareTime, payload.description,payload.mt1,
                payload.mt2,payload.mt3,payload.ad1,payload.ad3,payload.ad2,payload.profession,payload.school);
*/
        return ResponseEntity.status(200).body(RespEntity.code(200).body(userService.Certificate( payload.openId,payload.name,payload.weChatNumber, payload.phone, payload.email, payload.fee,payload.spareTime, payload.description,payload.mt1, payload.mt2,payload.mt3,payload.ad1,payload.ad3,payload.ad2,payload.profession,payload.school)).build());
    }

    @ApiOperation(value = "修改用户信息", notes = "参数\n" +
            " ")
    @PostMapping("/modify-user")
    public ResponseEntity modifyUser(@RequestBody UserPayload payload) {

        User user = userService.ModifyUser(payload.openId,payload.name,payload.gender,payload.inviteCode,payload.styleSign,payload.intro,payload.avatar);
        return ResponseEntity.status(200).body(RespEntity.code(200).body(user).build());

    }

    @ApiOperation(value = "我的浏览记录、提问、作品、收藏、回答、评论、二级评论", notes = "参数 完整的payload\n" +
            " ")
    @GetMapping("/my")
    public ResponseEntity my(@RequestParam String type,@RequestParam String openId ,@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size) {

        return ResponseEntity.status(200).body(RespEntity.code(200).body(recordService.myRecord(type,openId,PageRequest.of(page - 1, size))).build());
    }

    @ApiOperation(value = "我的浏览记录、提问、作品、收藏、回答、评论、二级评论", notes = "参数 完整的payload\n" +
            " ")
    @GetMapping("/my-unread")
    public ResponseEntity myUnread(@RequestParam String openId ,@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size) {

        return ResponseEntity.status(200).body(RespEntity.code(200).body(recordService.myUnreadRecord(openId,PageRequest.of(page - 1, size))).build());
    }

    @ApiOperation(value = "读取消息详情使其已读", notes = "参数 完整的payload\n" +
            " ")
    @GetMapping("/read")
    public ResponseEntity read(@RequestParam String id ) {

        return ResponseEntity.status(200).body(RespEntity.code(200).body(recordService.read(id)).build());
    }



    @Data
    private static class UserPayload {
        private String name;
        private String gender;
        private String inviteCode;
        private String openId;
        private String styleSign;
        private String intro;
        private String avatar;
    }

    @Data
    private static class VipUserPayload {
        String openId;
        String name;
        String weChatNumber;
        String phone;
        String email;
        String fee;
        String spareTime;
        String description;
        String mt1;
        String mt2;
        String mt3;
        String ad1;
        String ad3;
        String ad2;
        String profession;
        String school;
    }
}
